/*
 * Copyright 2010 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yannickstucki.android.musicqueue.ui;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.yannickstucki.android.musicqueue.R;
import com.yannickstucki.android.musicqueue.SongController;
import com.yannickstucki.android.musicqueue.communication.OnSongsUpdatedListener;
import com.yannickstucki.android.musicqueue.data.Song;
import com.yannickstucki.android.musicqueue.data.SongUpdateResult;
import com.yannickstucki.android.musicqueue.util.Logger;

/**
 * The main activity. Due to performance and rotation reasons, the app has only
 * only this main activity that can change shape.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class Screen extends Activity implements OnSongsUpdatedListener {

  /**
   * Screen is the only class that has a reference to songController. Even
   * though songController is a singleton, it mustn't be accessed anywhere else.
   */
  private SongController songController;

  /**
   * The playing screen mode is used in portrait. A reference is kept for
   * caching and thus having faster rotation.
   */
  private PlayingScreenMode playingScreenMode;

  /**
   * The choosing screen mode used in landscape. A reference is kept for caching
   * and thus having faster rotation.
   */
  private ChoosingScreenMode choosingScreenMode;
  
  /**
   * Manages all the context menus used in the choosing screen.
   */
  private ContextMenuManager manager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    Logger.out("activity on start");

    songController = SongController.getInstance();
    if (!songController.isInitialized()) {
      songController.initialize(this.getApplicationContext(), this);
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    IScreenMode screenMode;

    if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) {
      Logger.out("Set screen mode: Playing");
      this.setContentView(R.layout.playing_mode);
      if (playingScreenMode == null) {
        playingScreenMode = new PlayingScreenMode(songController.getSongCommander());
      }

      DragView<Song> dragView = (DragView<Song>) this.findViewById(R.id.dragview);
      dragView.setViewGenerator(new SongViewGenerator(this));
      dragView.setOnDragListener(playingScreenMode);

      playingScreenMode.setUiElements(dragView, (ImageButton) findViewById(R.id.playpause),
          (ImageButton) findViewById(R.id.shuffle), (Button) findViewById(R.id.queuesize));
      screenMode = playingScreenMode;
    } else {
      Logger.out("Set screen mode: Choosing");
      this.setContentView(R.layout.landscape);
      if (choosingScreenMode == null) {
        SongViewGenerator songViewGenerator = new SongViewGenerator(this);
        choosingScreenMode = new ChoosingScreenMode(songController.getSongEnqueuer(),
            new SongAdapter(this, songController.getAllSongs(), songViewGenerator),
            new SongAdapter(this, songController.getStarredSongs(), songViewGenerator));

       manager = new ContextMenuManager(choosingScreenMode, this.getResources());
      }
      ListView listView = (ListView) findViewById(R.id.list);
      this.registerForContextMenu(listView);
      choosingScreenMode.setUiElements(listView, (ImageButton) findViewById(R.id.allsongs),
          (ImageButton) findViewById(R.id.starredsongs));
      screenMode = choosingScreenMode;
    }
    songController.setOnUiUpdateListener(screenMode.getOnUiUpdateListener());
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    return manager.contextItemSelected(item);
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    manager.createContextMenu(menu, v, menuInfo);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    Logger.out("activity on destroy");
    songController.onActivityDestroyed();
  }

  @Override
  public void onSongsUpdated(SongUpdateResult result) {
    onConfigurationChanged(this.getResources().getConfiguration());
  }
}
